use lazy_static::lazy_static;
use rbase::db::DbColumnDefine;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "t_task_knowledge")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,

    #[sea_orm(default_value = 0)]
    pub task_id: i64,

    #[sea_orm(default_value = 0)]
    pub knowledge_id: i64,

    #[sea_orm(default_value = 0)]
    pub created_at: Option<i64>,

    #[sea_orm(default_value = 0)]
    pub modified_at: Option<i64>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation, Deserialize, Serialize)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::knowledge::Entity",  // 关联到 Knowledge 实体
        from = "Column::KnowledgeId",
        to = "super::knowledge::Column::Id"
    )]
    Knowledge, // 关系名称
    #[sea_orm(
        belongs_to = "super::task::Entity",  // 关联到 Task 实体
        from = "Column::TaskId",
        to = "super::task::Column::Id"
    )]
    Task, // 关系名称
}

impl ActiveModelBehavior for ActiveModel {} // 必须实现这个trait

pub static TABLE_NAME: &str = "t_task_knowledge";
pub static CREATE_QUERY: &str = r#"
    CREATE TABLE IF NOT EXISTS t_task_knowledge (
        id              INTEGER NOT NULL PRIMARY KEY,
        task_id         INTEGER NOT NULL,
        knowledge_id    INTEGER NOT NULL,
        created_at      INTEGER NOT NULL,
        modified_at     INTEGER NOT NULL,
        UNIQUE (task_id, knowledge_id)
    );
"#;

lazy_static! {
    pub static ref TABLE_COLUMNS: Vec<DbColumnDefine<'static>> = vec![];
}

pub fn get_column(name: &String) -> Column {
    match name.as_str() {
        "id" => Column::Id,
        "task_id" => Column::TaskId,
        "knowledge_id" => Column::KnowledgeId,
        "created_at" => Column::CreatedAt,
        "modified_at" => Column::ModifiedAt,
        _ => Column::Id,
    }
}
